home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-23 | 4.2 KB | 142 lines | [TEXT/ALFA] |
- \
- \
- \ PF Forms Handler: Template -- version 1.3.2
- \
- \
- \ (c) Ronald T. Kneusel, 1995, 1996
- \ (rkneusel@post.its.mcw.edu)
- \
- \ This code may be used and distributed freely provided the copyright
- \ notice remains intact and my name is mentioned in the documentation.
- \
- \ Last mod: 23-Apr-96
- \ =========================================================================
- \
- \ This file assumes these files to be already loaded (in this order):
- \
- \ server.4th - web server interface
- \ field.4th - field definition and access words
- \
- \ This code handles the reading and evaluation of the template HTML file.
- \
- \ A template file is an external text file that contains the HTML source to
- \ be returned by the CGI with named markers where calculated values are to
- \ be substituted:
- \
- \ <h1>A Reply</h1><hr>
- \ You are: `name` <p>
- \ Age: `age` <p>
- \ Weight: `weight` <p>
- \
- \ The above will look for fields or named strings with the names "name", "age",
- \ and "weight" and substitute their value in the reply string.
-
- \ *** None of these words check for overflow or error conditions! Memory is
- \ at a premium, so you, the programmer, are on your own!
-
-
- 1024 constant buffSize \ size of input buffer
- create rwbuff buffSize allot \ the buffer
- variable ~i \ start by filling the buffer
- variable ~f \ number of bytes into the file
-
- : fillbuff ( -- ) \ call this after OPEN to use the buffer
- 1024 ~i ! 0 ~f ! rwbuff buffSize 0 fill ;
-
- : getch ( -- c ) \ read a character from the buffer
- ~i @ dup 1024 < IF
- rwbuff + c@ 1 ~i +! 1 ~f +!
- ELSE
- drop 0 ~i ! \ clear index
- rwbuff buffSize 0 fill
- @size ~f @ - 3 - dup 1 < IF
- drop -1 \ nothing to read, return -1
- ELSE
- buffSize min \ # bytes to read
- rwbuff swap READ
- rwbuff c@ 1 ~i +!
- THEN
- THEN ;
-
- ( string array words )
-
- : array>> ( #elements -- ) \ create an array of #elements
- create 2* allot ;
-
- : !array ( data index array -- ) \ store in index
- swap 2* + ! ;
-
- : @array ( index array -- data ) \ get data in an array
- swap 2* + @ ;
-
- : >array ( 00 01 .. n array -- ) \ store entire array
- swap -1 swap 1- DO
- dup rot swap r swap !array
- -1 +LOOP ;
-
- \ add these to the number convert
-
- : n>str ( n s -- ) \ integer to string
- >r 0 d>f r> 0 fix f>str ;
-
- : str>num ( s -- n ) \ string to integer
- str>f f>d drop ;
-
- variable ~output \ output string
- variable ~fname \ filename string
- variable ~length \ hold length of output string
-
- create !@# 32 allot \ hold the name
- create #@! 0 , 0 , \ null string
-
- : >!@# ( c -- ) \ append a character to !@#
- !@# length !@# + dup >r c! \ character
- 0 r> 1+ c! ; \ null
-
- : token ( -- ) \ load !@# with name of token
- 0 !@# c! \ clear !@#
- BEGIN
- getch dup 96 = 0= \ while not ` (backquote)
- WHILE >!@# REPEAT \ append to !@#
- drop
- ;
-
- : lookup ( -- addr ) \ lookup the token name and return address of text part
- #T# @ 0 DO
- r @(T) .name !@#
- $= IF
- r @(T) .text \ got it
- 10000
- ELSE 1 THEN
- +LOOP
- ;
-
- : >>output ( c -- ) \ append a character
- ~output @ ~length @ + dup >r c! 0 r> 1+ c! 1 ~length +! ; macro
-
- : template ( output fname new|append -- )
- \ process a template file
- >r ~fname ! ~output ! r>
- IF 0 ~output @ c! 0 ~length ! \ zero the string
- ELSE ~output @ length ~length ! THEN \ append to string
- ~fname @ open \ open the file
- fillbuff \ init input buffer
- @size 0 DO
- getch \ get a character
- dup 96 = IF \ check for ` (backquote)
- drop \ lose the `
- token \ get the name
- lookup \ and lookup the string
- ~output @ strcpy \ and put it in
- ~output @ length ~length ! \ adjust length
- !@# length 1+ \ length of name+1 (for backquote)
- ELSE
- dup 31 > IF
- >>output \ append to output
- ELSE drop THEN 1
- THEN
- +LOOP
- close \ close the file
- ;
-
-